if(!Gfa){
	Gfa = {ConditionHelper:{}};
}else{
	$.extend(Gfa,{ConditionHelper:{}});
}
Gfa.ConditionHelper = {
	getFields4Form : function(formSelector){
		return $("input,select",formSelector);
	},
	buildConditionByFormId : function(formId) {
		return Gfa.ConditionHelper.buildCondition("#"+formId);
	},
	buildConditionByFormName : function(formName) {
		return Gfa.ConditionHelper.buildCondition("form[name='"+formName+"']");
	},
	buildCondition : function(formSelector) {
		var formFields = Gfa.ConditionHelper.getFields4Form(formSelector);
		var fields = [];
		for ( var index = 0; index < formFields.length; index++) {
			var $formfield = $(formFields[index]);
			if(!$formfield.attr("condition") 
					|| $formfield.attr("condition").length < 1
					|| !$formfield.attr("name")
					|| $formfield.attr("name").length < 1
					|| !$formfield.val()
					|| $formfield.val().length < 1){
				continue;
			}
			if ($formfield.attr("name") && $formfield.attr("name").length > 0) {
				if($formfield.attr("type") == 'radio'){
					if(!$formfield.attr("checked") || $formfield.attr("checked")=='false' || $formfield.attr("checked")===false){
						continue;
					}
				}
				if($formfield.attr("type") == 'checkbox'){
					var formatRules = $.parseJSON($formfield.attr("formatRules"));
					if(!$.isEmptyObject(formatRules) 
							&& $.isPlainObject(formatRules)
							&& !$.isEmptyObject(formatRules.checkedValueMap) 
							&& $.isPlainObject(formatRules.checkedValueMap)){
						//checked 当复选框用作单选作用时,勾选复选框是value应为true/1/yes或其它,反选复选框时以为false/0/no.
						//<k:checkbox formatRules="{checkedValueMap:{checked:true,unChecked:false}}"/>,
						//<k:checkbox formatRules="{checkedValueMap:{checked:1,unChecked:0}}"/>,
						//<k:checkbox formatRules="{checkedValueMap:{checked:yes,unChecked:no}}"/>,
						//和jq-format-impl.js checkedValueMap 实现方法对应
					} else if(!$formfield.attr("checked") || $formfield.attr("checked")=='false' || $formfield.attr("checked")===false){
						continue;
					}
				}
				
				var field = {
						"fieldName" : $formfield.attr("name"),
						"fieldValueString" : $formfield.val(),
						"fieldParam" : $formfield.attr("name") + "_" + index,
						"condition" : $formfield.attr("condition").length<1 ? "eq" : $formfield.attr("condition")
					};
				if($formfield.attr("fromAssociateObject") && $formfield.attr("fromAssociateObject").length > 0){
					$.extend(field,{"fromAssociateObject":$formfield.attr("fromAssociateObject")});
				}
				if($formfield.attr("fromAssociateIdObject") && $formfield.attr("fromAssociateIdObject").length > 0){
					$.extend(field,{"fromAssociateIdObject":$formfield.attr("fromAssociateIdObject")});
				}
				if($formfield.attr("fromAssociateCollection") && $formfield.attr("fromAssociateCollection").length > 0){
					$.extend(field,{"fromAssociateCollection":$formfield.attr("fromAssociateCollection")});
				}
				
				//日期控件 的值转换(只支持到  年月,年月日,的查询粒度)
				if ($formfield.attr("dateFormat") && $formfield.attr("dateFormat")) {
					var fieldDate = new Date(Date.parse(Gfa.ConditionHelper.processDate(field.fieldValueString,field.condition).replace(/-/g, "/")));
					field.fieldValueString = Gfa.ConditionHelper.formatDateTime(fieldDate);
				}
				fields.push(field)
			}
		}
		return fields
	},
		
	buildCondition4Form : function(formId) {
		var formFields = Gfa.ConditionHelper.getFields4Form("#"+formId);
		var fields = [];
		for ( var index = 0; index < formFields.length; index++) {
			var objInput = formFields[index];
			var name = objInput.name;
			if (name && name.indexOf("_gqf") != -1 && !(objInput.id && objInput.id.indexOf("_code_name_display") != -1 || name.indexOf("_code_name_config") != -1)) {
				var fName = name.substring(0, name.indexOf("_gqf"));
				var fValue = objInput.value;
				var cValueString = Gfa.ConditionHelper.getCondition(formFields,fName, index);
				var cValue = "eq";
				if (cValueString && cValueString.indexOf("condition") != -1) {
					cValue = cValueString.substring(10)
				}
				if (fValue && fValue != "") {
					var field = {};
					field.fieldName = fName;
					field.fieldValueString = fValue;
					field.fieldParam = fName + "_" + index;
					field.condition = cValue;
					var id = objInput.id;
					if (id && id.indexOf("datePicker") != -1) {
						fValue = Gfa.ConditionHelper.processDate(fValue,field.condition);
						var d = new Date(Date.parse(fValue.replace(/-/g, "/")));
						field.fieldValueString = d.getTime();
						if (field.condition == "le" || field.condition == "lt") {
							field.condition = "le";
							var fieldTo = {};
							fieldTo.fieldName = fName;
							fieldTo.fieldValueString = (new Number(field.fieldValueString)+ 24 * 60 * 60 * 1000)-1;
							field.fieldValueString = fieldTo.fieldValueString;
							fieldTo.fieldParam = fName + "_" + index + "_to";
							fieldTo.condition = "le";
							fields.push(fieldTo)
						}
					}
					fields.push(field)
				}
			}
		}
		return fields
	},
	buildCondition4Form4Fros : function(formId) {
		var fields = [];
		var condition = {} ;
		var searchForm = document.getElementById(formId);
		var children = searchForm.elements;
		var length = children.length;
		for ( var index = 0; index < length; index++) {
			var objInput = children[index];
			var name = objInput.name;
			if (name && name.indexOf("_gqf") != -1 && !(objInput.id && objInput.id.indexOf("_code_name_display") != -1 || name.indexOf("_code_name_config") != -1)) {
				var fName = name.substring(0, name.indexOf("_gqf"));
				var fValue = objInput.value;
				var cValueString = Gfa.ConditionHelper.getCondition(children,fName, index);
				var cValue = "eq";
				if (cValueString && cValueString.indexOf("condition") != -1) {
					cValue = cValueString.substring(10)
				}
				if (fValue && fValue != "") {
					var field = {};
					field.fieldName = fName;
					field.fieldValueString = fValue;
					field.fieldParam = fName + "_" + index;
					field.condition = cValue;
					var id = objInput.id;
					if (id && id.indexOf("datePicker") != -1) {
						var date = new Date(Date.parse(fValue.replace(/-/g, "/")));
						if (field.condition == "le" || field.condition == "lt") {
							date.setDate(date.getDate()+1);
							date.setMilliseconds(date.getMilliseconds()-1);
							field.fieldParam = fName + "_" + index + "_to";	
						}
						field.fieldValueString = Gfa.ConditionHelper.formatDateTime(date);
					}
					fields.push(field) ;
					condition[field.fieldName] = field.fieldValueString ;
				}
			}
		}
		return condition ;
	},	
	getCondition : function(fields, fieldName, start) {
		var len = fields.length;
		var cFieldName = fieldName + "_gci";
		for ( var index = start; index < len; index++) {
			var field = fields[index];
			if (cFieldName == field.name) {
				return field.value
			}
		}
	},
	formatDateTime : function (date) {
		var year=date.getFullYear();
		var month=date.getMonth()+1;
		var day=date.getDate();
		var hours=date.getHours();
		var minuts=date.getMinutes();
		var seconds=date.getSeconds();
		if(month<10) month="0"+month;
		if(day<10) day="0"+day;
		if(hours<10) hours="0"+hours;
		if(minuts<10) minuts="0"+minuts;
		if(seconds<10) seconds="0"+seconds;
		return year+"-"+month+"-"+day+" "+hours+":"+minuts+":"+seconds;
	},


	processDate : function(fValue,condition){
		//处理yyyy
		//起始是2000返回2000-01-01
		//截至是2000返回2000-12-31
		if(fValue.match(/(\d\d\d\d)/) && fValue.length<=4){
			//起始时间
			if(condition == Gfa.GlobalConstant.CONDITION_GE || condition == Gfa.GlobalConstant.CONDITION_GT){
				fValue += "-01-01";
			}
			//截止时间
			if(condition == Gfa.GlobalConstant.CONDITION_LE || condition == Gfa.GlobalConstant.CONDITION_LT){
				fValue += "-12-31";
			}
		}
		//处理yyyy-MM
		//起始是2000-01返回2000-01-01
		//截至是2000-01返回2000-01-31
		if(fValue.match(/(\d\d\d\d)[-./](1[0-2]|0?[1-9])/) && fValue.length<=7){
			//起始时间
			if(condition == Gfa.GlobalConstant.CONDITION_GE || condition == Gfa.GlobalConstant.CONDITION_GT){
				fValue += "-01";
			}
			
			//截止时间
			if(condition == Gfa.GlobalConstant.CONDITION_LE || condition == Gfa.GlobalConstant.CONDITION_LT){
				var tempStr = fValue + "-01";
				var d = new Date(Date.parse(tempStr.replace(/-/g, "/")));
				fValue += "-"+d.getDaysInMonth();
			}
		}
		//处理yyyy-MM-dd
		//起始是2000-01-01返回2000-01-01 00:00:00
		//截至是2000-01-01返回2000-01-01 23:59:59
		if(fValue.match(/(\d\d\d\d)[-./](1[0-2]|0?[1-9])[-./](0?[1-9]|1[0-9]|2[0-9]|3[0-1])/) && fValue.length<=10){
			//起始时间
			if(condition == Gfa.GlobalConstant.CONDITION_GE || condition == Gfa.GlobalConstant.CONDITION_GT){
				fValue += " 00:00:00";
			}
			
			//截止时间
			if(condition == Gfa.GlobalConstant.CONDITION_LE || condition == Gfa.GlobalConstant.CONDITION_LT){
				fValue += " 23:59:59";
			}
		}
		
		//处理yyyy-MM-dd HH
		//起始是2000-01-01 00返回2000-01-01 00:00:00
		//截至是2000-01-01 00返回2000-01-01 00:59:59
		if(fValue.match(/(\d\d\d\d)[-./](1[0-2]|0?[1-9])[-./](0?[1-9]|1[0-9]|2[0-9]|3[0-1])[ ](0?[0-9]|1[0-9]|2[0-4])/) && fValue.length<=13){
			//起始时间
			if(condition == Gfa.GlobalConstant.CONDITION_GE || condition == Gfa.GlobalConstant.CONDITION_GT){
				fValue += ":00:00";
			}
			
			//截止时间
			if(condition == Gfa.GlobalConstant.CONDITION_LE || condition == Gfa.GlobalConstant.CONDITION_LT){
				fValue += ":59:59";
			}
		}
		
		//处理yyyy-MM-dd HH:mm
		//起始是2000-01-01 00:00返回2000-01-01 00:00:00
		//截至是2000-01-01 00:00返回2000-01-01 00:00:59
		if(fValue.match(/(\d\d\d\d)[-./](1[0-2]|0?[1-9])[-./](0?[1-9]|1[0-9]|2[0-9]|3[0-1])[ ](0?[0-9]|1[0-9]|2[0-4])[:](0?[0-9]|[0-5][0-9])/) && fValue.length<=16){
			//起始时间
			if(condition == Gfa.GlobalConstant.CONDITION_GE || condition == Gfa.GlobalConstant.CONDITION_GT){
				fValue += ":00";
			}
			
			//截止时间
			if(condition == Gfa.GlobalConstant.CONDITION_LE || condition == Gfa.GlobalConstant.CONDITION_LT){
				fValue += ":59";
			}
		}
		return fValue;
	}
};


$.extend(Date, {
    daysInMonth : [31,28,31,30,31,30,31,31,30,31,30,31]
});

$.extend(Date.prototype, {
	getDaysInMonth : function() {
		Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
		return Date.daysInMonth[this.getMonth()];
	},
	isLeapYear : function() {
        var year = this.getFullYear();
        return !!((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year)));
    }
});
